﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Data;
using Top.Web;

namespace Top.PointStore.Business
{
    public static class MemoryTable
    {
        private static ConcurrentDictionary<string, T_OAuthInfo> t_OAuthInfo = new ConcurrentDictionary<string, T_OAuthInfo>();

        private static ConcurrentDictionary<string, T_PointMain_new> t_PointMain_new = new ConcurrentDictionary<string, T_PointMain_new>();

        private static ConcurrentDictionary<int, T_Broker> t_Broker = new ConcurrentDictionary<int, T_Broker>();

        private static ConcurrentDictionary<int, S_Admin> s_Admin = new ConcurrentDictionary<int, S_Admin>();

        private static List<T_ProductShowExpandV3> t_ProductShow = new List<T_ProductShowExpandV3>();

        private static ConcurrentDictionary<int, T_ProductShowExpandV3> t_ProductShow_Plus = new ConcurrentDictionary<int, T_ProductShowExpandV3>();

        private static ConcurrentDictionary<int, int> crmUserCity = new ConcurrentDictionary<int, int>();

        public static ConcurrentDictionary<string, T_OAuthInfo> TB_OAuthInfo
        {
            get
            {
                return t_OAuthInfo;
            }
            set
            {
                t_OAuthInfo = value;
            }
        }

        public static ConcurrentDictionary<string, T_PointMain_new> TB_PointMain_new
        {
            get
            {
                return t_PointMain_new;
            }
            set
            {
                t_PointMain_new = value;
            }
        }

        public static ConcurrentDictionary<int, T_Broker> TB_Broker
        {
            get
            {
                return t_Broker;
            }
            set
            {
                t_Broker = value;
            }
        }

        public static ConcurrentDictionary<int, S_Admin> TB_Admin
        {
            get
            {
                return s_Admin;
            }
            set
            {
                s_Admin = value;
            }
        }

        public static List<T_ProductShowExpandV3> TB_ProductShow
        {
            get
            {
                return t_ProductShow;
            }
            set
            {
                t_ProductShow = value;
            }
        }

        public static ConcurrentDictionary<int,T_ProductShowExpandV3> TB_ProductShow_Plus
        {
            get
            {
                return t_ProductShow_Plus;
            }
            set
            {
                t_ProductShow_Plus = value;
            }
        }

        public static ConcurrentDictionary<int, int> Map_CrmUserCity
        {
            get
            {
                return crmUserCity;
            }
            set
            {
                crmUserCity = value;
            }
        }

        public static void Init()
        {
            try
            {
                Init_OAuthInfo_table().Wait();
            }
            catch(Exception ex)
            {
                ErrorLog.WriteLog(ex);
                //Init_OAuthInfo_table().Wait();
            }
            try
            {
                Init_PointMain_table().Wait();
            }
            catch 
            {
                Init_PointMain_table().Wait();
            }
            try
            {
                Init_Broker_table().Wait();
            }
            catch
            {
                Init_Broker_table().Wait();
            }
            try
            {
                Init_Admin_table().Wait();
            }
            catch
            {
                Init_Admin_table().Wait();
            }
            try
            {
                Init_ProductShow_table().Wait();
            }
            catch
            {
                Init_ProductShow_table().Wait();
            }
            try
            {
                Init_CrmCity_table().Wait();
            }
            catch
            {
                Init_CrmCity_table().Wait();
            }                                    
        }

        public async static Task<int> Init_OAuthInfo_table()
        {
            TB_OAuthInfo.Clear();
            //List<T_OAuthInfo> oauthlist = new List<T_OAuthInfo>();
            string sql = @"select * from t_oauthinfo";
            using (var db = DataBaseContext.GetDataBase("reader"))
            {
                var results = await db.Sql(sql).ToList<T_OAuthInfo>();
                foreach (T_OAuthInfo item in results)
                {
                    TB_OAuthInfo.TryAdd(item.F_UserToken.ToUpper(), item);
                }

            }
            return 1;
        }

        public async static Task<int> Init_PointMain_table()
        {
            TB_PointMain_new.Clear();
            List<T_PointMain_new> pointMainlist = new List<T_PointMain_new>();
            string sql = @"select * from t_pointMain_new";
            using (var db = new DbTour())
            {
                pointMainlist = await db.Sql(sql).ToList<T_PointMain_new>();
            }
            foreach (T_PointMain_new item in pointMainlist)
            {
                TB_PointMain_new.TryAdd(item.F_UserToken.ToUpper(), item);
            }
            if (pointMainlist.Count != TB_PointMain_new.Count)
            {
                return 0;
            }
            return 1;
        }

        public async static Task<int> Init_Broker_table()
        {
            TB_Broker.Clear();
            List<T_Broker> brokerList = new List<T_Broker>();
            string sql = @"
                SELECT
	                a.kid,
	                a.F_CityKid,
	                a.F_Title,
	                a.F_Phone,
	                a.F_AddTime,
	                a.F_EditTime,
	                a.F_BrokerCompanyKid,
	                a.F_BrokerCompanyName,
	                ifnull(b.F_IsDistribute, 0) AS IsFenXiao
                FROM
	                t_broker a
                LEFT JOIN t_brokercompany b ON a.F_BrokerCompanyKid = b.Kid
                ";
            using (var db = new DbTour())
            {
                brokerList = await db.Sql(sql).ToList<T_Broker>();
            }
            foreach (T_Broker item in brokerList)
            {
                TB_Broker.TryAdd(item.Kid, item);
            }
            if (brokerList.Count != TB_Broker.Count)
            {
                return 0;
            }
            return 1;
        }

        public async static Task<int> Init_Admin_table()
        {
            TB_Admin.Clear();
            List<S_Admin> adminList = new List<S_Admin>();
            string sql = @"
                SELECT
	                kid,
	                F_RoleID,
	                F_RealName,
	                F_CellPhone,
	                F_AddTime,
	                F_EditTime
                FROM
	                s_admin
                ";
            using (var db = new DbTour())
            {
                adminList = await db.Sql(sql).ToList<S_Admin>();
            }
            foreach (S_Admin item in adminList)
            {
                TB_Admin.TryAdd(item.Kid, item);
            }
            if (adminList.Count != TB_Admin.Count)
            {
                return 0;
            }
            return 1;
        } 

        public async static Task<int> Init_ProductShow_table()
        {
            t_ProductShow_Plus.Clear();
            List<T_ProductShowExpandV3> productShowList = new List<T_ProductShowExpandV3>();
            using (var db = new DbTour())
            {
                string sql = @"
                    SELECT
	                    iFNULL(F_CityKid, 0) AS F_CityKid,
	                    Kid,
	                    F_Sort,
	                    F_ShowName,
	                    F_AddedTime,
	                    F_SoldoutTime,
	                    F_ProductKid,
	                    F_ProductName,
	                    F_PicUrl,
	                    F_BeginTime,
	                    F_EndTime,
	                    F_PointNum,
	                    F_IsMeskill,
	                    F_ExchangeBeginTime,
	                    F_ExchangeEndTime,
	                    F_InventoryNum,
	                    ExchangeNum
                    FROM
	                    V_ProductShow
                    ";
                productShowList = await db.Sql(sql).ToList<T_ProductShowExpandV3>();
                foreach (var item in productShowList)
                {
                    item.F_EndTime = item.F_EndTime.AddDays(-1);
                    item.F_SoldoutTime = item.F_SoldoutTime.AddDays(-1);
                }
            }
            TB_ProductShow = productShowList;
            foreach (T_ProductShowExpandV3 item in TB_ProductShow)
            {
                t_ProductShow_Plus.TryAdd(item.F_ProductKid, item);
            }
            return 1;
        }

        public async static Task<int> Init_CrmCity_table()
        {
            Map_CrmUserCity.Clear();
            List<CrmCityMapping> crmcityList = new List<CrmCityMapping>();
            string sql = @"
            select  
            a.kid,
            isnull(c.F_CityKid,0) as F_CityKid
            from S_Admin a 
            left join S_AdminRole b on a.Kid=b.F_AdminKid
            left join S_Framework c on b.F_FrameworkKid=c.Kid
            ";           
            using (var db = new DbTour("sqldefault"))
            {
                crmcityList = await db.Sql(sql).ToList<CrmCityMapping>();
            }
            foreach (CrmCityMapping item in crmcityList)
            {
                Map_CrmUserCity.TryAdd(item.Kid, item.F_CityKid);
            }
            return 1;
        }

        public async static Task<T_Broker>GetBroker(int brokerKid)
        {
            string sql = @"
                SELECT
	                a.kid,
	                a.F_CityKid,
	                a.F_Title,
	                a.F_Phone,
	                a.F_AddTime,
	                a.F_EditTime,
	                a.F_BrokerCompanyKid,
	                a.F_BrokerCompanyName,
	                ifnull(b.F_IsDistribute, 0) AS IsFenXiao
                FROM
	                t_broker a
                LEFT JOIN t_brokercompany b ON a.F_BrokerCompanyKid = b.Kid
                where a.kid = {0}
                ";
            using (var db = new DbTour())
            {
                return await db.Sql(sql,brokerKid).FirstOrDefault<T_Broker>();
            }
        }

        public async static Task<CrmCityMapping> GetCityKid(int crmKid)
        {
            string sql = @"
            select  
            a.kid,
            isnull(c.F_CityKid,0) as F_CityKid
            from S_Admin a 
            left join S_AdminRole b on a.Kid=b.F_AdminKid
            left join S_Framework c on b.F_FrameworkKid=c.Kid
            where a.kid = {0}
            ";
            using (var db = new DbTour("sqldefault"))
            {
                return await db.Sql(sql,crmKid).FirstOrDefault<CrmCityMapping>();
            }
        }
    }

    public class CrmCityMapping
    {
        public int Kid { get; set; }

        public int F_CityKid { get; set; }
    }
}
